package com.example.demofdfs.web;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demofdfs.util.Command;
import com.example.demofdfs.util.CommandPool;

import io.swagger.annotations.Api;
import net.neoremind.sshxcute.core.Result;

@RestController
@RequestMapping("/shell")
@Api(tags = "shell 执行")
public class ShellController extends BaseController {

	Logger logger = LoggerFactory.getLogger(ShellController.class);
	private static char space = ' ';
	private static char mark = '"';
	
//	@Resource(name = "shellCommand")
//	private Command shellCommand;
	
	@GetMapping("/exec")
	public Result exec(String cmd, String ip) {
		if (ip != null)
			cmd = ansibleCmd(cmd, ip);
		logger.warn(cmd);
		Result result = CommandPool.exec(cmd);
		return result;
	}
	// ansible 192.168.225.143 -m shell -a "ps -ef | grep java"
	private String ansibleCmd(String cmd, String ip) {
		StringBuilder sb = new StringBuilder("ansible");
		sb.append(space).append(ip).append(space).append("-m shell -a").append(space)
		.append(mark).append(cmd).append(mark);
		
		return sb.toString();
	}
}
